package com.ruoyi.job.task;

import com.ruoyi.job.domain.CuoWu;
import com.ruoyi.job.feign.PublisherClient;
import com.ruoyi.job.service.JudgeAndSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component("rySendTask")
public class RySendTask {
    @Autowired
    private JudgeAndSendService judgeAndSendService;
    @Autowired
    private PublisherClient publisherClient;
    //查警告并发给交换机
        public void judgeAndSend() {
        // 找到需要发送的警告
        List<CuoWu> list = judgeAndSendService.find();
        System.out.println("找到的警告有：" + list);
        //拼接项目部名+时间并发送
        List<String> finalList = new ArrayList<>();
        for (CuoWu cuoWu : list) {
            finalList.add(cuoWu.getProjectName()+cuoWu.getRecordTime());
        }
        //将finalList中的字符串以逗号拼接
        String joinedMessage = String.join(",", finalList);
        String message = joinedMessage + "数据有报错";
        String exchangeName = "tledu.direct";
        //查出错误才发
        if(!message.equals("数据有报错"))
        {
            //发送消息
            System.out.println("向生产者发送的消息为：" + message);
            publisherClient.sendEmail(exchangeName, message);
        }

    }
}
